package com.gzhryc.shared_device.oem.h5.mng.controllers.agent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.json.JsonTools;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.oem.code.agent.dao.db.AgentType;
import com.gzhryc.shared_device.oem.code.agent.dao.db.AgentTypeMenuRel;
import com.gzhryc.shared_device.oem.code.agent.services.AgentMenuService;
import com.gzhryc.shared_device.oem.code.agent.services.AgentTypeMenuRelService;
import com.gzhryc.shared_device.oem.code.agent.services.AgentTypeService;
import com.gzhryc.system.dto.MenuData;

public class AgentTypeMenuController {

    @Route(value="/agent/type/menu/config.html",method="get")
    public String editMenu(HttpServletRequest request, HttpServletResponse response){
        Long id = WebUtils.getLong(request,"id");
        if(id != null){
            AgentType agentType = AgentTypeService.self().getById(id);
            if(agentType != null){
                request.setAttribute("agentType",agentType);
                List<AgentTypeMenuRel> list = AgentTypeMenuRelService.self().findJoinByTypeId(id);
                List<MenuData> data = new ArrayList<MenuData>();
                if(list != null && list.size() > 0){
                    Map<Long,MenuData> map = new HashMap<Long,MenuData>();
                    for(AgentTypeMenuRel item : list){
                        MenuData temp = new MenuData();
                        temp.setId(item.getMenu().getId());
                        temp.setName(item.getMenu().getName());
                        temp.setChildren(new ArrayList<>());
                        map.put(temp.getId(), temp);  //临时保存
                        if(item.getMenu().getParentId() == null){
                            data.add(temp);
                        }else{
                            MenuData parent = map.get(item.getMenu().getParentId());
                            if(parent != null){
                                parent.getChildren().add(temp);
                            }
                        }
                    }
                }
                request.setAttribute("data", JsonTools.toJson(data));
                //获取所以根菜单
                request.setAttribute("rootMenuList", AgentMenuService.self().findRoot());
            }
        }
        return "/agent/type/menu.html";
    }

    @SuppressWarnings("unchecked")
    @Route(value="/agent/type/menu/config.do",method="post")
    public AjaxResult doEditMenu(HttpServletRequest request,HttpServletResponse response){
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"保存失败");
        Long typeId = WebUtils.getLong(request,"typeId");
        String data = request.getParameter("data");
        if(typeId != null && StringUtils.isNotBlank(data)){
            List<Map<String,Object>> list = JsonTools.fromJson(data, List.class);
            try {
                if(AgentTypeMenuRelService.self().save(typeId, list)){
                    result.setCode(DtoConstants.SUCCESS, null);
                }
            } catch (LogicException e) {
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }

    
}
